﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.Strings;
using WFNetLib.GDI;
using System.IO;

namespace MakePicture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] bs = StringsFunction.strToHexByte(textBox1.Text, " ");
                Image img = WFImage.byteArrayToImage(bs);
                pictureBox1.Width = img.Width;
                pictureBox1.Height = img.Height;
                pictureBox1.Image = img;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] bs = StringsFunction.strToHexByte(textBox1.Text, " ");
                Image img = WFImage.byteArrayToImage(bs);
                pictureBox1.Width = img.Width;
                pictureBox1.Height = img.Height;
                pictureBox1.Image = img;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {                    
                    FileInfo f = new FileInfo(saveFileDialog1.FileName);
                    if (!Directory.Exists(f.DirectoryName))
                        Directory.CreateDirectory(f.DirectoryName);
                    FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(bs);
                    //清空缓冲区
                    bw.Flush();
                    //关闭流
                    bw.Close();
                    fs.Close();/**/
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
